﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using QLCONGNOLIBS.Ultils;
using QLCONGNOLIBS.Securities;
using QLCONGNOLIBS.Models.EntityFW;

namespace QLCONGNOLIBS.Models
{
    public class UsersModels : BaseModels<tblUser>
    {        
        //1. Khai báo các thủ tục sẽ sử dụng
        private const string CHECK_LOGIN = "SP_CHECKLOGIN";
        private const string GET_HOC_VIEN = "SP_HIEN_THI_THANHVIEN";
        private const string INSERT_HOC_VIEN = "SP_INSERT_THANHVIEN";
        private const string UPDATE_HOC_VIEN = "SP_UPDATE_THANHVIEN";
        private const string DELETE_HOC_VIEN = "SP_DELETE_THANHVIEN";

         //3. Hàm khởi dựng - Constructors
        public UsersModels(SqlConnection objConnection, SecurityEntity objSecurity)
                    : base(objConnection, objSecurity){ }

        public UsersModels(string connectString) : base(connectString){ }                

        #region "1. Thực thi và nhận về dữ liệu là DataTable"
        protected internal override DataTable GetElements()
        {
            DataTable objTable = new DataTable();
            try
            {
                m_objCommand = new SqlCommand(GET_HOC_VIEN, m_objConnection);
                m_objCommand.CommandType = CommandType.StoredProcedure;
                m_objAdapter = new SqlDataAdapter(m_objCommand);
                m_objAdapter.Fill(objTable);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
            }
            return objTable;
        }        
        #endregion

        #region "2. Thực thi nghiệp vụ kiểm tra tài khoản"
        /// <summary>
        /// Thực thi nghiệp vụ kiểm tra tài khoản
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        protected internal override DataTable CheckElement(tblUser obj)
        {
            DataTable objTable;         
            try
            {                
                SqlParameter[] param = new SqlParameter[2];
                param[0] = (new BUltils()).CreateParammeter("@userName", obj.Username);
                param[1] = (new BUltils()).CreateParammeter("@passWord", SecurityConexts.EncryptInfo(obj.Password));

                //Thực thi truy vấn thông qua Store Procedure
                objTable = new DataTable();                
                objTable = ExecProcReturnTable(CHECK_LOGIN, param);

                //True
                if (objTable.Rows.Count > 0)
                {
                    DataRow row = objTable.Rows[0];
                    m_objSecurity.LoginSuccess = true;
                    m_objSecurity.UserName = obj.Username;
                    m_objSecurity.FullName = "" + row["FullName"];
                    m_objSecurity.Email = "" + row["Email"];
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
            }
            return objTable;           
        }
        #endregion
    }
}
